<?php

/*
	Class: Cts_Db_Table_Abstract

	About: Author
		Jaybill McCarthy

	About: License
		<http://communit.as/docs/license>

	About: See Also
		- Zend_Db_Table
*/
abstract class Cts_Db_Table_Abstract extends Zend_Db_Table {

        public $_module_id = "default";

	/* Group: Constructors */

	/*
		Constructor: Cts_Db_Table_Abstract
			Calls the parent's constructor and instantiates a logger. Also sets up a DB adapter and the _cts_plugin variable.

		Arguments:
			config (optional) - TBD
	*/
    function Cts_Db_Table_Abstract($config = null) {
    	$this->_logger = Zend_Registry::get('logger');
    	if (isset($this->_use_adapter)) {
    		$dbAdapters = Zend_Registry::get('dbAdapters');
    		$config = ($dbAdapters[$this->_use_adapter]);
    	}
    	$this->_cts_plugin = new Cts_Plugin;
    	return parent::__construct($config);
    }

	/* Group: Instance Methods */
    
	/*
		Function: getCountByWhereClause
			Gets a count of records using the supplied where clause.

		Arguments:
			whereclause (optional) - A string with your where clause in it. The word "where" is not needed. If you provide no where clause, a count of all rows will be returned.

		Returns:
			An integer representing the total number of records for that where clause.
	*/
    public function getCountByWhereClause($whereclause = null) {
    	$db = $this->getAdapter();
    	$sql = "SELECT count(*) from " . $this->_name;  
    	if (!is_null($whereclause)) {
    		$sql .= " where " . $whereclause;
    	}
    	$total = $db->fetchOne($sql);
    	return $total;
    }

/*
    public function insert(array $data)
    {
		$metadata = $this->info();
		$columns = $metadata['cols'];
		$timestamp = date("Y-m-d H:i:s") ;
		if(in_array('created_on',$columns)){			
			if(!in_array('created_on',$data)){
				$data['created_on'] = $timestamp;
			}
		}

       if(in_array('updated_on',$columns)){			
			if(!in_array('updated_on',$data)){
				$data['updated_on'] = $timestamp;
			}
		}
    
        return parent::insert($data);
    }
*/   

	/*
		Function: update
			Updates the table with the supplied data array. You must supply a where clause if you want to limit what gets updated, otherwise all rows will get the update.

		Arguments:
			data - An array with a key-value pair for each column in the table.
			where - A where clause string to limit the rows that get updated. If the where clause is empty, all rows will be updated with the data argument.

		Returns:
			TBD
	*/
    public function update(array $data, $where) {	
    	$metadata = $this->info();
		$columns = $metadata['cols'];
		$timestamp = date("Y-m-d H:i:s") ;
    	if (in_array('updated_on', $columns)) {
			if (!in_array('updated_on', $data)) {
				$data['updated_on'] = $timestamp;
			}
		}
    	return parent::update($data, $where);
    }

	protected function _T($key, $replace = null) {		
		return Cts_Translate($this->_module_id, $key, $replace);
	}

}
